package com.ruoyi.web.controller.file;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.file.domain.File;
import com.ruoyi.file.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 模型分类Controller
 * 
 * @author zbw
 * @date 2022-07-20
 */
@RestController
@RequestMapping("/file/updown")
public class FileController extends BaseController
{
    @Autowired
    private IFileService fileService;

    @Autowired
    private UploadUtil uploadUtil;

    @Value("${app.download-path}")
    private String download;


    @PreAuthorize("@ss.hasPermi('file:updown:upload')")
    @Log(title = "上传", businessType = BusinessType.EXPORT)
    @PostMapping("/upload")
    public com.ruoyi.web.controller.file.AjaxResult uploadImage(@RequestParam("file") MultipartFile[] files) throws Exception {
        return uploadUtil.uploadImages(files);
    }

    /**
     * 下载文件
     */
    @PreAuthorize("@ss.hasPermi('file:updown:download')")
    @Log(title = "下载", businessType = BusinessType.EXPORT)
    @PostMapping("/download")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException {

        //获得请求文件名
        String filename = request.getParameter("filename");

        //设置文件MIME类型
        response.setContentType("application/download;utf-8");
        //设置Content-Disposition
        response.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes("utf-8"), "iso-8859-1"));
        //读取目标文件，通过response将目标文件写到客户端
        //获取目标文件的绝对路径
        String fullFileName = download + filename;
        //读取文件
        InputStream in = new FileInputStream(fullFileName);
        OutputStream out = response.getOutputStream();

        //写文件
        int b;
        while((b=in.read())!= -1)
        {
            out.write(b);
        }

        in.close();
        out.close();
    }

    /**
     * 查询文件列表
     */
    @PreAuthorize("@ss.hasPermi('file:updown:list')")
    @GetMapping("/list")
    public TableDataInfo list(File file)
    {
        startPage();
        List<File> list = fileService.selectFileList(file);
        return getDataTable(list);
    }

    /**
     * 导出文件列表
     */
    @PreAuthorize("@ss.hasPermi('file:updown:export')")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, File file)
    {
        List<File> list = fileService.selectFileList(file);
        ExcelUtil<File> util = new ExcelUtil<File>(File.class);
        util.exportExcel(response, list, "文件数据");
    }

    /**
     * 获取文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('file:updown:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") String fileId)
    {
        return AjaxResult.success(fileService.selectFileByFileId(fileId));
    }

    /**
     * 新增文件
     */
    @PreAuthorize("@ss.hasPermi('file:updown:add')")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody File file)
    {
        return toAjax(fileService.insertFile(file));
    }

    /**
     * 修改文件
     */
    @PreAuthorize("@ss.hasPermi('file:updown:edit')")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody File file)
    {
        return toAjax(fileService.updateFile(file));
    }

    /**
     * 删除文件
     */
    @PreAuthorize("@ss.hasPermi('file:updown:remove')")
    @Log(title = "文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable String[] fileIds)
    {
        return toAjax(fileService.deleteFileByFileIds(fileIds));
    }


}
